﻿using Ewell.BizComponent.Charge.UI.VM;
using Ewell.BizComponent.Service.Server;
using Ewell.Interface.Accounting.Common.Model.Request;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ewell.BizComponent.Charge.UI
{
    /// <summary>
    /// DataSelectFrm.xaml 的交互逻辑
    /// </summary>
    public partial class DataSelectFrm : Page
    {
        public DataSelectVm vm { get; set; }
        public DataSelectFrm()
        {
            InitializeComponent();
        }

        public DataSelectFrm(DataSelectVm vm)
        {
            this.vm = vm;
            this.DataContext = vm;
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key is Key.Enter or Key.Space or Key.Up or Key.Down or Key.Left or Key.Right) return;
            ComboBox comboBox = sender as ComboBox;
            DataSelectItemVm dataSelectItem= comboBox.Tag as DataSelectItemVm;
            var insurServer = new InsurServer();
            var basicDatas = insurServer.SearchDataSelectItem(new RequestHeader() { interfaceId = long.Parse(this.vm.context.patientVm.interfaceId), cardInfo = this.vm.context.basicInfo, alreadySelectDataList = this.vm.context.alreadySelectDataList }, new DataSelectItemQueryRequest() { basicDataDictTypeId = long.Parse(dataSelectItem.basicDataDictTypeId), basicDataDictTypeNo = dataSelectItem.basicDataDictTypeNo, currentBizParams = this.vm.currentBizParams, currentBizTransactionCoding = this.vm.context.outPreSettlementRequest.settlementTypeCode == "1" ? "101" : "201" ,keyword= comboBox.Text});
            var dataSelectItemVm=this.vm.dataSelectItemList.FirstOrDefault(f => f.basicDataDictTypeId == dataSelectItem.basicDataDictTypeId);
            if(dataSelectItemVm != null)
            {
                dataSelectItemVm.basicDataList = new List<BasicDataVm>();
                if (basicDatas!=null&& basicDatas.Count()>0)
                {
                    foreach (var item in basicDatas)
                    {
                        dataSelectItemVm.basicDataList.Add(new BasicDataVm()
                        {
                            basicDataDictTypeId= dataSelectItemVm.basicDataDictTypeId,
                            basicDataDictTypeNo= dataSelectItemVm.basicDataDictTypeNo,
                            basicDataId= item.basicDataId,
                            basicDataNo= item.basicDataNo,
                            basicDataName= item.basicDataName,
                            basicDataExtInfo= item.basicDataExtInfo,
                            pinyin= item.pinyin,
                            wubi= item.wubi,
                        });
                    }
                }
            }
            comboBox.ItemsSource = dataSelectItemVm.basicDataList;
            comboBox.IsDropDownOpen = true;
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            (sender as ComboBox).Text = ((sender as ComboBox).SelectedItem as BasicDataVm)?.basicDataName;
        }
    }
}
